import { reqGoodsInfo, reqAddOrUpdateShopCart } from '@/api';
//封装游客的uuid，只是用一次
import { getUUID } from '@/utils/uuid_token.js';
//detail模块小仓库
const state = {
  goodInfo: {},
  //游客的临时身份，uuid
  uuid_token: getUUID()
}
const mutations = {
  GETGOOGINFO(state, goodInfo) {
    state.goodInfo = goodInfo
  }
}
const actions = {
  //获取产品详情
  async getGoodInfo({ commit }, skuId) {
    let result = await reqGoodsInfo(skuId)
    if (result.code === 200) {
      commit('GETGOOGINFO', result.data)
    }
  },
  //将产品添加到购物车中
  async addOrUpdateShopCart({ commit }, { skuId, skuNum }) {
    const result = await reqAddOrUpdateShopCart(skuId, skuNum)
    if (result.code == 200) { return 'ok' }
    else {
      return Promise.reject(new Error('faile'))
    }
  }
}
const getters = {
  categoryView(state) {
    return state.goodInfo.categoryView || {}
  },
  skuInfo(state) {
    return state.goodInfo.skuInfo || {}
  },
  spuSaleAttrList(state) {
    return state.goodInfo.spuSaleAttrList || []

  }
}

export default {
  state,
  mutations,
  actions,
  getters
}